package com.buu.education.controller;

import com.alibaba.fastjson.JSON;
import com.buu.common.annotation.Log;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.education.domain.EducationCommentDO;
import com.buu.education.domain.EducationEvaluateDO;
import com.buu.education.service.EducationCommentService;
import com.buu.education.service.EvaluateService;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.system.domain.SysUserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价区间维护
 */
@RequestMapping("/education_comment")
@Controller
public class EducationCommentController {

    @Autowired
    private EducationCommentService educationCommentService;

    /**
     * 评价内容页面
     * @param model
     * @return
     */
    @GetMapping("/edu_com")
    String eduCom(Model model) {
        return "evaluate/comment/view";
    }

    /**
     * 评价列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        List<EducationCommentDO> eduList = educationCommentService.list();
        PageUtils pageUtil = new PageUtils(eduList, eduList.size());
        return pageUtil;
    }

    /**
     * 新增评价区间
     * @param model
     * @return
     */
    @GetMapping("/add")
    String add(Model model) {
        return "evaluate/comment/add";
    }

    /**
     * 保存评价
     * @param educationCommentDO
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    R save(EducationCommentDO educationCommentDO) {

        if (educationCommentService.save(educationCommentDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除评价
     * @param id
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    Map<String, Boolean> remove(Long id) {
        Map<String, Boolean> rMap = new HashMap<>();
        rMap.put("success", educationCommentService.remove(id) > 0);
        return rMap;
    }

    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        EducationCommentDO educationCommentDO = educationCommentService.get(id);
        model.addAttribute("eduComm", educationCommentDO);
        return "evaluate/comment/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    R update(EducationCommentDO educationCommentDO) {
        if(educationCommentService.update(educationCommentDO) > 0){
            return R.ok();
        }
        return R.error();
    }
}
